package com.czp.role.service.impl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;

import com.czp.framework.model.DataStoreTemplate;
import com.czp.framework.util.DateUtils;
import com.czp.framework.util.JsonUtils;
import com.czp.role.mapper.IRoleMapper;
import com.czp.role.model.RelationMenu;
import com.czp.role.model.Role;
import com.czp.role.service.IRoleService;
import com.google.gson.Gson;

/**
 * 【环 境】：J2SE 1.7
 * @author   柴志鹏	CHAIZP@GMAIL.COM
 * @version  version 1.0
 * @since    2014-6-16
 */
public class RoleServiceImpl implements IRoleService {
	private IRoleMapper roleMapper;
	private Gson gson = new Gson();
	
	public String getRoleList(Role role) throws Exception {
		DataStoreTemplate dst = new DataStoreTemplate();
		dst.setList(roleMapper.getRoleList(role, new RowBounds(role.getStart(), role.getLimit())));
		dst.setTotal(roleMapper.getRoleRecords(role));
		return gson.toJson(dst);
	}
	
	public int roleOperator(String jsonData) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(jsonData);
		if(jsonArr == null || jsonArr.isEmpty()) {
			return 0;
		}
		Role role = new Role();
		for(int i = 0; i < jsonArr.size(); i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			JsonUtils.jsonToBean(jsonObj, role);
			String cudType = jsonObj.getString("cudType");
			role.setModifyDate(DateUtils.getSysTimestamp());
			if(cudType.equals("C")) {//添加
				role.setCreateDate(DateUtils.getSysTimestamp());
				roleMapper.insertRole(role);
			} else if(cudType.equals("U") && !"".equals(role.getId())) {//修改
				roleMapper.updateRole(role);
			} else if(cudType.equals("D") && !"".equals(role.getId())) {//删除
				roleMapper.deleteRole(role);
			} else {
				continue;
			}
		}
		return jsonArr.size();
	}
	
	public String getRelationMenuList(Role role) throws Exception {
		DataStoreTemplate dst = new DataStoreTemplate();
		RowBounds bounds = new RowBounds(role.getStart(), role.getLimit());
		dst.setList(roleMapper.getRelationMenuList(role, bounds));
		dst.setTotal(roleMapper.getRelationMenuRecords(role));
		return gson.toJson(dst);
	}
	
	public int deleteRelationMenu(String jsonData) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(jsonData);
		if(jsonArr == null || jsonArr.isEmpty()) {
			return 0;
		}
		RelationMenu relMenu = new RelationMenu();
		int i = 0;
		for(int cnt = jsonArr.size(); i < cnt; i++) {
			JsonUtils.jsonToBean(jsonArr.getJSONObject(i), relMenu);
			roleMapper.deleteRelationMenu(relMenu);
		}
		return i;
	}
	
	public int relationMenuController(String jsonData) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(jsonData);
		if(jsonArr == null || jsonArr.isEmpty()) {
			return 0;
		}
		RelationMenu relMenu = new RelationMenu();
		int i = 0;
		for(int cnt = jsonArr.size(); i < cnt; i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			JsonUtils.jsonToBean(jsonObj, relMenu);
			if(relMenu.getChecked().equals("true")) {
				if(roleMapper.matchAddRelationMenu(relMenu) == 0) {
					roleMapper.insertRelationMenu(relMenu);
				}
			} else {
				roleMapper.deleteRelationMenu(relMenu);
			}
		}
		return i;
	}
	
	public String queryRelationMenu(RelationMenu relMenu) throws Exception {
		return gson.toJson(
				roleMapper.queryRelationMenu(relMenu));
	}
	
	/**
	 * 获取 roleMapper
	 * @return 返回 roleMapper
	 */
	public IRoleMapper getRoleMapper() {
		return roleMapper;
	}

	/**
	 * 设置 roleMapper
	 * @param 对roleMapper进行赋值
	 */
	public void setRoleMapper(IRoleMapper roleMapper) {
		this.roleMapper = roleMapper;
	}

}
